package com.xuxueli.applyModules.utils;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.xuxueli.applyModules.exception.ErrorException;
import org.apache.commons.io.FileUtils;

import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 测试生成json文件
 */
public class TestJsonFile {

    public static void main(String[] args) {
        String msgId = "EWFWMS";
        try {
            testPacket(msgId);
        }catch (Exception e){
            System.out.println(e.getMessage());
        }
        String dirPath = "E://测试上传"+File.separator + msgId + ".json";
        readJson(dirPath);
    }

    private static void testPacket(String msgId) {
        //1、组装表头
        String typeName = "InvtHeadType";
        HashMap<String, Object> headMap = new HashMap();
        try {
            headMap.put("ApplyNo","6668798");
            headMap.put("ApplyTime","2021-03-31");
            headMap.put("ApplyName","各位深刻反思AV");
            headMap.put("IsExist","true");
            packageMsgJson(msgId, headMap, typeName);
        } catch (Exception ex) {
            throw new ErrorException(500, "组装表头失败:" + ex.getMessage());
        }
        //2组装表头
        typeName = "InvtListType";
        List<Map<String, Object>> list = new ArrayList();
        try {
            HashMap<String, Object> listMap = new HashMap();
            HashMap<String, Object> listMaps = new HashMap();
            listMap.put("ListNo","6668798");
            listMap.put("ListTime","2021-03-31");
            listMap.put("ListName","各位深刻反思AV");
            listMap.put("IsExist","true");
            listMaps.put("ListNo","6668798");
            listMaps.put("ListTime","2021-03-31");
            listMaps.put("ListName","各位深刻反思AV");
            listMaps.put("IsExist","true");
            list.add(listMap);
            list.add(listMaps);
            packageListMsgJson(msgId, list, typeName);
        } catch (Exception ex) {
            throw new ErrorException(400, "组装表体失败:" + ex.getMessage());
        }
        //3、组装测试
        typeName = "InvtHeadTest";
        HashMap<String, Object> testMap = new HashMap();
        try {
            testMap.put("TestNo","88988989");
            testMap.put("TestTime","2021-03-31");
            testMap.put("TestName","测试组装test数据格式");
            testMap.put("IsExist","true");
            packageMsgJson(msgId, testMap, typeName);
        } catch (Exception ex) {
            throw new ErrorException(400, "组装测试失败:" + ex.getMessage());
        }
    }

    private static void packageListMsgJson(String msgId, List<Map<String,Object>> list, String typeName)throws Exception {
        for (int i = 0; i < list.size(); i++) {
            packageMsgJson(msgId,list.get(i),typeName);
        }
    }

    private static void  packageMsgJson(String msgId,Map<String,Object> map,String typeName) throws Exception {
        String dirPath = "E://测试上传";
        if (map == null) {
            return;
        }
        File dirFile = new File(dirPath);
        FileUtils.forceMkdir(dirFile);
        String filePath = dirPath + File.separator + msgId + ".json";
        File file = new File(filePath);

        StringBuffer sb = new StringBuffer();
        if (file.exists()){
            sb.append("\r\n");
        }
        sb.append(String.format("{\"%s\":", typeName));
        sb.append(JSONArray.toJSONString(map, SerializerFeature.WriteMapNullValue));
        sb.append("}");
        String msg=sb.toString().replace("null","\"\"");
        FileUtils.write(file, msg, "utf-8", true);
    }

    private static void readJson(String filePath){
        try {
            File file=new File(filePath);
            if(file.isFile() && file.exists()){ //判断文件是否存在
                InputStreamReader read = new InputStreamReader(new FileInputStream(file),"utf-8");//考虑到编码格式
                BufferedReader bufferedReader = new BufferedReader(read);
                String lineTxt = null;
                StringBuilder sb = new StringBuilder();
                while((lineTxt = bufferedReader.readLine()) != null){
                    System.out.println(lineTxt);
                }
                read.close();
            }else{
                System.out.println("找不到指定的文件");
            }
        } catch (Exception e) {
            System.out.println("读取文件内容出错");
            e.printStackTrace();
        }
    }
}